<template>
  <div class="login-container">
    <div class="box">
      <div class="out">
        <div class="logo"></div>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules2"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            type="username"
            v-model="ruleForm.username"
            prefix-icon="el-icon-user-solid"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.password"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="submit"
            @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    var validatePass2 = (rule, value, callback) => {
      const reg = /^\d{6,}$/
      if (!reg.test(value)) {
        callback(new Error('请输入6位及6位以上长度的密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: 'admin',
        password: '123456',
      },
      rules2: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          // { validator: validatePass2, trigger: 'blur' }
          { min: 5, max: 11, message: '长度在5到11个字符', trigger: ['blur', 'change'] }
        ]
      }
    };
  },
  methods: {
    submitForm (ruleForm) {
      this.$refs[ruleForm].validate(async (valid) => {
        console.log(valid, 'valid');
        if (valid) {
          await this.$store.dispatch('user/asyncLogin', this.ruleForm)
          this.$router.push('/')
        } else {
          // 最少有一个不满足校验
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>
<style lang='less'  scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: #2b4b6b;
  .box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    width: 450px;
    height: 300px;
    .out {
      position: absolute;
      left: 30%;
      top: -25%;
      width: 180px;
      height: 180px;
      box-shadow: 1px 1px 12px #888888;
      border-radius: 90px;
      background-color: #fff;
      .logo {
        position: absolute;
        left: 9%;
        top: 8%;
        background-color: pink;
        width: 150px;
        height: 150px;
        background: url(@/assets/logo.png) no-repeat center / cover;
        background-color: #eeeeee;
        border-radius: 75px;
      }
    }
    .el-form {
      margin: 120px auto;
      width: 90%;
    }
    .el-form-item:nth-child(3) {
      padding-left: 255px;
    }
  }
}
</style>
